package com.gscitysfy.cus.drain.modules.warning.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cus.drain.modules.common.entity.DrainInspectionMnFile;
import com.gscitysfy.cus.drain.modules.inspection.service.IDrainInspectionMnFileService;
import com.gscitysfy.cus.drain.modules.warning.entity.DrainSystemCommandLead;
import com.gscitysfy.cus.drain.modules.warning.entity.DrainWaringSystem;
import com.gscitysfy.cus.drain.modules.warning.mapper.DrainSystemCommandLeadMapper;
import com.gscitysfy.cus.drain.modules.warning.mapper.DrainWaringSystemMapper;
import com.gscitysfy.cus.drain.modules.warning.service.IDrainSystemCommandLeadService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqiang
 * @since 2022-10-23 17:38:32
 */
@Service
public class DrainSystemCommandLeadServiceImpl extends ServiceImpl<DrainSystemCommandLeadMapper, DrainSystemCommandLead> implements IDrainSystemCommandLeadService {
    @Autowired
    DrainSystemCommandLeadMapper drainSystemCommandLeadMapper;
    @Autowired
    IDrainInspectionMnFileService drainInspectionMnFileService;

    @Override
    public Result<Object> getByPage(Page<DrainSystemCommandLead> page, DrainSystemCommandLead drainSystemCommandLead) {
        IPage<DrainSystemCommandLead> drainSystemCommandLeadList =drainSystemCommandLeadMapper.getPageList(page,drainSystemCommandLead);
        return Result.content(drainSystemCommandLeadList);
    }

    @Override
    public Result<Object> getListByQuery(DrainSystemCommandLead drainSystemCommandLead) {
        if(drainSystemCommandLead.getCmKid()!=null){
            List<DrainSystemCommandLead> drainSystemCommandLeadList =drainSystemCommandLeadMapper.getPageList(drainSystemCommandLead);
            if(CollectionUtils.isEmpty(drainSystemCommandLeadList)){
                return Result.content(null);
            }else{
                return Result.content(drainSystemCommandLeadList.get(0));
            }
        }else{
            List<DrainSystemCommandLead> drainWaringSystemList =drainSystemCommandLeadMapper.getPageList(drainSystemCommandLead);
            return Result.content(drainWaringSystemList);

        }

    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Result<Boolean> del(DrainSystemCommandLead drainSystemCommandLead) {
        drainSystemCommandLead = drainSystemCommandLeadMapper.selectById(drainSystemCommandLead.getCmKid());
        drainSystemCommandLead.setCmUsable(true);
        Long fileId = drainSystemCommandLead.getSystemFileId();
        drainSystemCommandLeadMapper.updateById(drainSystemCommandLead);
        if(fileId!=null){
            DrainInspectionMnFile drainInspectionMnFile = new DrainInspectionMnFile();
            drainInspectionMnFile.setCmKid(fileId);
            drainInspectionMnFile.setCmUsable(true);
            drainInspectionMnFileService.updateById(drainInspectionMnFile);
        }
        return Result.content(true);
    }


    /**
     * 新增或者修改预警机制
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Result<Boolean> addOrUpdate(DrainSystemCommandLead drainSystemCommandLead) {

        return Result.content(this.saveOrUpdate(drainSystemCommandLead));
    }
}
